package com.movie.act;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.movie.bean.MovieCinema;
import com.movie.bean.MovieNew;
import com.movie.bean.MovieNewList;
import com.movie.bean.TimeGrid;
import com.movie.util.Log;
import com.movie.util.RoateUtil;

/**
 * 26-影院排期
 * 
 */
public class MoviePrepareAct extends BaseActivity {
	private Context mContext = MoviePrepareAct.this;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.movieprepareact);
		// movieprepareact
		getBundle();
		initBackTitleBar();
		findViewByIds();
		onClick();
		initMoreMovies();
	}

	private void getBundle() {
		Intent mIntent = getIntent();
		mMovieCinema = (MovieCinema) mIntent
				.getSerializableExtra("MovieCinema");
	}

	private MovieCinema mMovieCinema = null;

	private void findViewByIds() {

		cinemaname = (TextView) findViewById(R.id.cinemaname);
		if (mMovieCinema != null) {
			cinemaname.setText(mMovieCinema.name);
		}

		scorerate = (RatingBar) findViewById(R.id.scorerate);
		scoretext = (TextView) findViewById(R.id.scoretext);
		pinluntext = (TextView) findViewById(R.id.pinluntext);
		// bottom = (LinearLayout) findViewById(R.id.bottom);
	}

	private ImageView yingyuanxiangqing = null;
	// private ImageView duihuangquan = null;
	private TextView moviedes = null;

	private void onClick() {
		yingyuanxiangqing = (ImageView) findViewById(R.id.yingyuanxiangqing);
		yingyuanxiangqing.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, CinemaDesAct.class);
				mContext.startActivity(intent);
			}
		});
		// duihuangquan = (ImageView) findViewById(R.id.duihuangquan);
		// duihuangquan.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// Intent intent = new Intent(mContext, BuyTicketAct.class);
		// mContext.startActivity(intent);
		// }
		// });
		moviedes = (TextView) findViewById(R.id.moviedes);
		moviedes.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, MovieDesAct.class);
				mContext.startActivity(intent);
			}
		});
		onDateClick();

	}

	private int dateIndex = 0;
	private String[] dateArray = new String[] { "1月12日(今)", "1月13日(明)",
			"1月14日(后)" };
	private ImageView jian = null;
	private ImageView jia = null;
	private TextView zhangshu = null;

	private HashMap<String, ArrayList<TimeGrid>> timetable = new HashMap<String, ArrayList<TimeGrid>>();

	private void onDateClick() {

		for (int j = 0; j < dateArray.length; j++) {
			ArrayList<TimeGrid> times = new ArrayList<TimeGrid>();
			for (int i = 0; i < 12; i++) {
				times.add(new TimeGrid("1" + (j + 2) + ":"+"1" + (j + 2) ));
			}
			timetable.put(dateArray[j], times);
		}
		jian = (ImageView) findViewById(R.id.jian);
		jia = (ImageView) findViewById(R.id.jia);
		zhangshu = (TextView) findViewById(R.id.zhangshu);

		jia.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				addLogic();
			}

		});
		;
		jian.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				decreaseLogic();
			}

		});
		setZhangshu();

		// 初始化时间表
		zhangshu.setText(dateArray[0] + "");
		times.clear();
		times=new ArrayList<TimeGrid>(timetable.get(dateArray[0]));
		Log.i("----------MoviePrepareAct.onDateClick()---times.size"
				+ times.size());
		Log.i("----------MoviePrepareAct.onDateClick()---timetable"
				+ timetable.get(dateArray[0]).size());
		initGridView();
	}

	private void decreaseLogic() {
		if (dateIndex > 0) {
			dateIndex -= 1;
			zhangshu.setText(dateArray[dateIndex] + "");
			times.clear();
			updateGridView();
			times=new ArrayList<TimeGrid>(timetable.get(dateArray[dateIndex]));
			updateGridView();
			if (dateIndex == 0) {
				jia.setVisibility(View.VISIBLE);
				jian.setVisibility(View.GONE);
			} else if (dateIndex < 2) {
				jia.setVisibility(View.VISIBLE);
			}
		}
	}

	private void setZhangshu() {
		if (dateIndex == 0) {
			jia.setVisibility(View.VISIBLE);
			jian.setVisibility(View.GONE);
		} else if (dateIndex == 2) {
			jia.setVisibility(View.GONE);
			jian.setVisibility(View.VISIBLE);
		}
	}

	private void addLogic() {
		if (dateIndex < 2) {
			dateIndex += 1;
			times.clear();
			updateGridView();
			times=new ArrayList<TimeGrid>(timetable.get(dateArray[dateIndex]));
			updateGridView();
			zhangshu.setText(dateArray[dateIndex]);
			if (dateIndex == 2) {
				jia.setVisibility(View.GONE);
				jian.setVisibility(View.VISIBLE);
			} else if (dateIndex > 0) {
				jian.setVisibility(View.VISIBLE);
			}
			// jian.setBackgroundResource(backgrounds[1]);
			// jian.setEnabled(false);
		}
	}

	// -------------------影片列表-----------START
	private void initMoreMovies() {
		more_listview = (ListView) findViewById(R.id.more_listview);

		mMovieNewList = new MovieNewList();
		MovieNew mMovieNew = null;
		for (int i = 0; i < 10; i++) {
			mMovieNew = new MovieNew();
			mMovieNew.name = "龙门飞甲" + i;
			mMovieNew.imageUrl = "龙门飞甲" + i;
			mMovieNew.status = "0";
			mMovieNew.scorerate = i + "";
			mMovieNew.point = i + "分";
			mMovieNew.date = "11月29日上映";
			mMovieNewList.addMovieNew(mMovieNew);
		}
		mMovieNewListAdapter = new MovieNewListAdapter();
		more_listview.setAdapter(mMovieNewListAdapter);
		// more_listview.setOnItemSelectedListener(new OnItemSelectedListener()
		// {
		//
		// public void onItemSelected(AdapterView<?> mAdapterView,
		// View parent, int position, long id) {
		// mMovieNewList.index = position;
		// showMovieItem(mMovieNewList.mMovieNews.get(position));
		// mMovieNewListAdapter.notifyDataSetChanged();
		// }
		//
		// public void onNothingSelected(AdapterView<?> arg0) {
		// }
		// });
		more_listview.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> mAdapterView, View parent,
					int position, long id) {
				mMovieNewList.index = position;
				showMovieItem(mMovieNewList.mMovieNews.get(position));
				mMovieNewListAdapter.notifyDataSetChanged();
			}
		});
		listview_menu = (ImageView) findViewById(R.id.listview_menu);
		listview_menu.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (ismListViewHidden) {
					more_listview.setVisibility(View.VISIBLE);
					ismListViewHidden = false;
				} else {
					more_listview.setVisibility(View.GONE);
					ismListViewHidden = true;
				}

			}
		});

	}

	private RatingBar scorerate = null;
	private TextView scoretext = null;
	private TextView pinluntext = null;
	private TextView cinemaname = null;

	private void showMovieItem(MovieNew bean) {
		Log.i("-----MainAct.showMovieItem-" + bean.point);
		scorerate.setRating(Float.parseFloat(bean.scorerate));
		scoretext.setText(bean.scorerate);
		pinluntext.setText(bean.pinluntext);
		cinemaname.setText(bean.name);
		// bigimage.setImageResource(bean.pinluntext);
	}

	private ImageView listview_menu = null;
	private boolean ismListViewHidden = true;
	private ListView more_listview = null;
	private MovieNewListAdapter mMovieNewListAdapter = null;
	private MovieNewList mMovieNewList = null;

	public class MovieNewListAdapter extends BaseAdapter {

		private LayoutInflater layoutFlater;

		public MovieNewListAdapter() {
			super();
			layoutFlater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public int getCount() {
			return mMovieNewList.mMovieNews.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			final ViewHolderListViewItem holder;
			if (convertView == null) {
				convertView = layoutFlater.inflate(
						R.layout.movie_new_list_item, null);
				holder = new ViewHolderListViewItem();
				holder.imageUrl = (ImageView) convertView
						.findViewById(R.id.imageUrl);
				holder.point = (TextView) convertView.findViewById(R.id.point);
				holder.date = (TextView) convertView.findViewById(R.id.date);
				holder.name = (TextView) convertView.findViewById(R.id.name);
				holder.name_orange = (TextView) convertView
						.findViewById(R.id.name_orange);
				holder.date_l = (LinearLayout) convertView
						.findViewById(R.id.date_l);
				holder.frame = (TextView) convertView.findViewById(R.id.frame);
				holder.pointbg = (TextView) convertView
						.findViewById(R.id.pointbg);
				RoateUtil.tranlateCenter(mContext, holder.point);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolderListViewItem) convertView.getTag();
			}
			MovieNew item = mMovieNewList.mMovieNews.get(position);
			String url = item.imageUrl;
			holder.imageUrl.setTag(url);
			ImageView cacheImage = holder.imageUrl;
			// final ListView listView = mListView;
			// NewInformationAct.mAsyncImageLoader.getListViewLogoItem(mContext,
			// url, cacheImage, listView, holder.mMyProcessbar);

			holder.point.setText(item.point);
			holder.date.setText(item.date);
			if ("0".equals(item.status))// 已上映
			{
				holder.date_l.setVisibility(View.INVISIBLE);

				holder.point.setVisibility(View.VISIBLE);
				holder.pointbg.setVisibility(View.VISIBLE);
				if (mMovieNewList.index == position) {
					holder.name_orange.setVisibility(View.VISIBLE);
					holder.name.setVisibility(View.GONE);
				} else {
					holder.name_orange.setVisibility(View.GONE);
					holder.name.setVisibility(View.VISIBLE);

				}
			}

			else if ("1".equals(item.status))// 未上映
			{
				holder.name.setVisibility(View.GONE);
				holder.name_orange.setVisibility(View.GONE);
				holder.date_l.setVisibility(View.VISIBLE);
				holder.point.setVisibility(View.INVISIBLE);
				holder.pointbg.setVisibility(View.INVISIBLE);
			}

			// 处理背景
			if (mMovieNewList.index == position) {
				holder.frame.setVisibility(View.VISIBLE);
			} else {
				holder.frame.setVisibility(View.GONE);
			}
			return convertView;
		}
	}

	private static class ViewHolderListViewItem {
		public ImageView imageUrl;
		public TextView point;
		public TextView date;
		public TextView pointbg;
		public TextView name;
		public TextView name_orange;
		public LinearLayout date_l;
		public TextView frame;

	}

	// -------------------影片列表----------END

	// 时间表-------------------------------------------S
	public void updateGridView() {
		mTimeGridAdapter.notifyDataSetChanged();
	}

	public void initGridView() {
//		for (int i = 0; i < 12; i++) {
//			times.add(new TimeGrid("8:" + i));
//		}
		if (times == null || times.size() == 0) {
			return;
		}
		if (time_grid != null) {
			mTimeGridAdapter.notifyDataSetChanged();
			return ;
		}
		time_grid = (GridView) findViewById(R.id.time_grid);
		mTimeGridAdapter = new TimeGridAdapter();
		time_grid.setAdapter(mTimeGridAdapter);
		time_grid.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if (times.get(arg2).enable) {
					times.get(arg2).enable = false;
					mTimeGridAdapter.notifyDataSetChanged();
				}

			}
		});

	}

	private ArrayList<TimeGrid> times = new ArrayList<TimeGrid>();
	private GridView time_grid = null;
	private TimeGridAdapter mTimeGridAdapter = null;

	public class TimeGridAdapter extends BaseAdapter {

		private LayoutInflater layoutFlater;

		public TimeGridAdapter() {
			super();
			layoutFlater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public int getCount() {
			return times.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			final ViewHolderGridViewItem holder;
			if (convertView == null) {
				convertView = layoutFlater.inflate(R.layout.grid_item, null);
				holder = new ViewHolderGridViewItem();
				holder.timegrid = (TextView) convertView
						.findViewById(R.id.timegrid);
				holder.timegrid_un = (TextView) convertView
						.findViewById(R.id.timegrid_un);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolderGridViewItem) convertView.getTag();
				holder.timegrid.setVisibility(View.INVISIBLE);
				holder.timegrid_un.setVisibility(View.GONE);
			}

			TimeGrid bean = times.get(position);
			holder.timegrid.setText(bean.time);
			if (bean.enable) {
				holder.timegrid.setVisibility(View.VISIBLE);
				holder.timegrid_un.setVisibility(View.GONE);
			} else {
				holder.timegrid.setVisibility(View.GONE);
				holder.timegrid_un.setVisibility(View.VISIBLE);
			}

			return convertView;
		}
	}

	private static class ViewHolderGridViewItem {
		public TextView timegrid;
		public TextView timegrid_un;
	}
	// 时间表-------------------------------------------E

}
